<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
$semf_root = "../../";


print_r($_GET);
//exit;
$action=$_GET['action'];
$table_id=$_GET['table_id'];
$module_name=$_GET['module_name'];

$inherited_module=$_GET['inherited_module'];
$inherited_table= $_GET['inherited_table'];
$project_home = $_GET['project_home'];

$relative_project_dir = $semf_root.$project_home;
$module_xml = $relative_project_dir.$module_name.'/module.xml';
$inherited_module_xml = $relative_project_dir.$inherited_module.'/module.xml';

//print_r($module_xml);
//exit;
$i=0;
$inherited=array();
if (file_exists($module_xml)) {
	//echo 'here';
	$doc_first = new DOMDocument();
	$doc_first->load($module_xml);
	
	$table_elements = $doc_first-> getElementsByTagName('table');
	
	$table_element = $table_elements -> item($table_id);
	$table_name = $table_element->getAttribute('name');// used in the below part
	
		$is_interface = $table_element->getAttribute("interface");
		if($is_interface == "yes") // never should be matched
		{
			//some code to handle interface -- anyway this shouldn't be displayed
			//echo 'interface';
			exit;
		}
		$child_elements = $table_element->childNodes;

		foreach ($child_elements as $child_element)
		{
			if($child_element->tagName != "inherited")
			{
				$i++;
				continue;
			}
			$inherited[$i++] = $child_element->nodeValue;
		}
		//print_r($inherited);
		//print_r($inherited_module.'.'.$inherited_table);
		//print_r(array_search($inherited_module.'.'.$inherited_table,$inherited));
		if ($action == 'add'){
			if( count($inherited) == 0 || (count($inherited) > 0 && !in_array($inherited_module.'.'.$inherited_table,$inherited) )){
				$tmp_new = $doc_first->createElement('inherited');
				$tmp_text = $doc_first->createTextNode($inherited_module.'.'.$inherited_table);
				$tmp_new -> appendChild($tmp_text);
				$table_element->appendChild($tmp_new);
			}
		}else if($action == 'remove'){
			$key =array_search($inherited_module.'.'.$inherited_table,$inherited);
			if( count($inherited) > 0 || $key){
				$table_element->removeChild($child_elements->item($key));
			}
		}
		$doc_first->save($module_xml);
}
//print_r($inherited);

//echo $inherited_module.'.'.$inherited_table;

$child_table=array();
if (file_exists($inherited_module_xml)) {
	$doc_second = new DOMDocument();
	$doc_second->load($inherited_module_xml);
	
	$tables = $doc_second-> getElementsByTagName('table');
	$index = 0;
	foreach ($tables as $element){
		//echo ($element->getAttribute('name'));
		if( $element -> getAttribute('name')  == $inherited_table ){	
			$inherited_table_id = $index;
			break;
		}
		$index ++;
	}
	if(!isset($inherited_table_id)){
		echo 'Fatal Error: Table not found';
		exit;
	}
	//echo $inherited_table_id;
	$target_element = $tables -> item($inherited_table_id);

	//$field_list = $target_table-> childNodes;
	if($target_element->getAttribute("interface")){
		$is_interface = $target_element->getAttribute("interface");
		if($is_interface != "yes") 
		{
				exit;
		}
	}
	$sub_elements = $target_element->childNodes;

	foreach ($sub_elements as $element)
	{
			if($element->nodeType != XML_ELEMENT_NODE )
			{
				continue;
			}
			$child_table[$i++] = $element->nodeValue;
	}
	$inherit_string=trim($module_name.'.'.$table_name);
	if ($action == 'add'){
		if( count($inherited) == 0 || (count($inherited) > 0 && !in_array($inherit_string,$child_table) )){
			$tmp_new = $doc_second->createElement('child');
			$tmp_text = $doc_second->createTextNode($inherit_string);
			$tmp_new -> appendChild($tmp_text);
			$target_element->appendChild($tmp_new);
		}
	}else if($action == 'remove'){
		$key =array_search($inherit_string,$child_table);
		if( count($inherited) > 0 || $key){
			$target_element->removeChild($sub_elements->item($key));
		}
	}
	$doc_second->save($inherited_module_xml);
}

//print_r($child_table);

?>